package made.util;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import made.util.Config.ACTION;
import made.util.Config.DEBUG_LEVEL;
import made.util.Config.STATE;
import made.util.Config;
import game.core.Game;

public class Helpers implements Config
{
    public static int toInt(boolean boolVal)
    {
        return boolVal ? 1 : 0;
    }

    public static String directionToString(int direction)
    {
        if (direction == Game.UP)
            return "UP";
        if (direction == Game.RIGHT)
            return "RIGHT";
        if (direction == Game.DOWN)
            return "DOWN";
        if (direction == Game.LEFT)
            return "LEFT";
        else
            return "UNDEFINED DIRECTION";
    }

    public static STATE toStateEnum(int value)
    {
        return STATE.class.getEnumConstants()[value];
    }

    public static ACTION toActionEnum(int value)
    {
        return ACTION.class.getEnumConstants()[value];
    }

    public static void appendStringToFile(String name, String text, boolean newLine)
    {
        BufferedWriter bufferedWriter = null;

        try
        {
            bufferedWriter = new BufferedWriter(new FileWriter(filepath + name + "_file.txt", true));

            bufferedWriter.write(text);

            if (newLine)
                bufferedWriter.newLine();
        } catch (FileNotFoundException ex)
        {
            ex.printStackTrace();
        } catch (IOException ex)
        {
            ex.printStackTrace();
        } finally
        {
            // Close the BufferedWriter
            try
            {
                if (bufferedWriter != null)
                {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
            } catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
};